/**
*@class Mouse
*@brief Class that controls one mouse
*@author De Schutter Bob
*@date 04-2011
* 
* @section source Sources
*
* @section working Reads input from one mouse and has buffer mechanism so MouseCC can read it's data
*
* @section destination mouseCC.cpp mouseCC.h
*
*/

#ifndef MOUSE_H
#define MOUSE_H

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include "thread.h"
#include "mouse.h"
#include "log.h"

#define BUFSIZE 3
#define BYTE 8
//#define CALIB



using namespace std;

class Mouse : public Thread
{
public:
    //lock the x and y values and start buffering new data
    void lock();
    //load buffered data in x and y values and reset buffer
    void unlock();
    //clear x and y data
    void reset();
    ///Called when thread is started. Identify mouse thread (with ident) and read data from that mouse device.
    void run();
    ///get the x-position of the mouse
    int getX();
    ///get the y-position of the mouse
    int getY();
    ///set the x-position of the mouse
    void setX(int offset);
    ///get the y-position of the mouse
    void setY(int offset);
    ///get the DPI setting of the mouse, not necessary when two identical mice
    void setDPI(int dpi);
    ///Constructor
    Mouse();
    ///Destructor
    ~Mouse();

private:
    ///used for calibration
    int xtot,ytot;
    ///x,y positions, mouse identifier and dpi difference between mice (if this mice has DPI/2 of the other mouse, dpi should be 2 here
    int x,y,ident,dpi_;
    ///x,y buffers
    int xbuf_,ybuf_;
    ///memory for storing the mouse data
    char mdata[BUFSIZE];
    ///to check if mouseCC is reading data
    bool lock_;
    ///pointer to log class
    Log *logger;
    ///stores string to mouse device (/dev/input/mouse0x)
    char mouse[20];
    ///pipe to mouse device
    ifstream pipe;

    ///Convert byte to string of 1's and 0's
    char* char2bin(char in);
    ///print all 3 bytes of the mouse data
    void printDataInfo();
    ///Increment counters etc
    void handleData(void); 
    ///function that starts the reading of the device with an infinte while loop (reason it has to be run in a thread)
    void readm(string mouse);
};
#endif
